<?php
/**
 * Created by PhpStorm.
 * User: Jesse
 * Date: 2018/3/1
 * Time: 18:17
 */

if (!function_exists('success')) {
    /**
     * return success json function
     *
     * @param  string  $message
     * @param  array|object|Illuminate\Database\Eloquent\Collection   $data
     * @param  array   $headers
     * @return \Illuminate\Http\JsonResponse
     */
    function success($message = '', $data = [], array $headers = [])
    {
        $code = 1;
        $status = 200;
        if (!is_array($data)) {
            if ($data instanceof Illuminate\Database\Eloquent\Collection) {
                $data = $data->toArray();
            } elseif(is_object($data)){
                $data = json_decode(json_encode($data), true);
            } else {
                $data = 'Data format is must be Collection 、 Array or Object';
            }
        }
        return response()->json(['code' => $code, 'msg' => $message, 'data' => $data], $status, $headers);
    }
}

if (!function_exists('error')) {
    /**
     * return error json function
     *
     * @param  int     $code
     * @param  string  $message
     * @param  array|Illuminate\Database\Eloquent\Collection   $data
     * @param  array   $headers
     * @return \Illuminate\Http\JsonResponse
     */
    function error($code, $message = '', $data = [], array $headers = [])
    {
        $status = 200;
        if (!is_array($data)) {
            if ($data instanceof Illuminate\Database\Eloquent\Collection) {
                $data = $data->toArray();
            } elseif(is_object($data)){
                $data = json_decode(json_encode($data), true);
            } else {
                $data = 'Data format is must be Collection 、 Array or Object';
            }
        }
        return response()->json(['code' => $code, 'msg' => $message, 'data' => $data], $status, $headers);
    }
}

if (!function_exists('sites')) {
    /**
     * 获取指定配置json文件
     * @param $key
     * @param string $default
     * @param string $name
     * @return mixed
     */
    function sites($key, $default = '', $name = 'sites') {
        if (!config()->get($name)) {
            $sites = json_decode(file_get_contents(storage_path('json/sites.json')), true);
            config()->set($name, $sites);
        }
        return config()->get($name . '.' . $key, $default);
    }
}

if (!function_exists('route_class')) {
    /**
     * 获取当前路由的路由命名
     * @return mixed
     */
    function route_class() {
        return str_replace('.', '-', Route::currentRouteName());
    }
}

if (!function_exists('qiniu_url')) {
    /**
     * 生成七牛资源的url
     * @param $url
     * @return string
     */
    function qiniu_url($url) {
        return 'http://' . env('QINIU_DOMAIN') . '/' . $url;
    }
}

if (!function_exists('make_excerpt')) {
    /**
     * 生成摘要，省略部分内容
     * @param $value
     * @param int $length
     * @return string
     */
    function make_excerpt($value, $length = 200) {
        $excerpt = trim(preg_replace('/\r\n|\r|\n+/', ' ', strip_tags($value)));
        return str_limit($excerpt, $length);
    }
}
